package com.aboo.pub.constant;

import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;

import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class ConfigXML {
	
	private static final Logger logger = Logger.getLogger(ConfigXML.class);
	private static Map<String, String> constantMap = new LinkedHashMap<String, String>();
	private static Set<String> adminUnFilterSet = new HashSet<String>();
	private static Set<String> portalUnFilterSet = new HashSet<String>();
	
	static {
		try {
			String path = Thread.currentThread().getContextClassLoader().getResource("") + "config/constants.xml";
			logger.info(path);
			SAXReader sax = new SAXReader();
			Document doc = sax.read(path);
			Element root = doc.getRootElement();
			
			Element constants = root.element("map").element("constants");
			Element constant = null;
			Iterator<Element> iter = constants.elementIterator("constant");
			while (iter.hasNext()) {
				constant = iter.next();
				constantMap.put(constant.attributeValue("id"), constant.getText());
			}
			
			Element adminUnFilters = root.element("set").element("adminUnFilters");
			Element adminUrl = null;
			iter = adminUnFilters.elementIterator("url");
			while (iter.hasNext()) {
				adminUrl = iter.next();
				adminUnFilterSet.add(adminUrl.getText());
			}
			
			Element portalUnFilters = root.element("set").element("portalUnFilters");
			Element portalUrl = null;
			iter = portalUnFilters.elementIterator("url");
			while (iter.hasNext()) {
				portalUrl = iter.next();
				portalUnFilterSet.add(portalUrl.getText());
			}
		} catch (DocumentException e) {
			logger.error("配置文件格式有误！", e);
		}		
	}

	public static Map<String, String> getConstantMap() {
		return constantMap;
	}
	public static void setConstantMap(Map<String, String> constantMap) {
		ConfigXML.constantMap = constantMap;
	}
	public static Set<String> getAdminUnFilterSet() {
		return adminUnFilterSet;
	}
	public static Set<String> getPortalUnFilterSet() {
		return portalUnFilterSet;
	}
	
}
